const checkTokenMiddleware = async (ctx, next) => {
    const path = ctx.request.path;
    if(path == "/login" || path == "/subinfor" || path == "/" || path == "/main" || path == "/minlogin" ) { 
        await next();
        return;
    }
    if( path == "/getinfor"|| path == "/minparent") {
        const { authorization } = ctx.request.header;
        let currentTimer = (new Date()).getTime();
        let selectToken = "select * from student where token=?";
        let result = await ctx.db.EXECUTE(selectToken, authorization);
        // 检查token是否在数据库中存在
        if (result.length > 0 && currentTimer < (new Date(result[0].timeout)).getTime()) {
            await next(); // token匹配，继续执行下一个中间件或路由处理函数
        } else {
        ctx.status = 401; // token不匹配，返回未授权状态码
            ctx.body = 'Invalid token';
        }
        return;
    }
    const { authorization } = ctx.request.header;
    let currentTimer = (new Date()).getTime();
    let selectToken = "select * from loginuser where token=?";
    let result = await ctx.db.EXECUTE(selectToken, authorization);
    // 检查token是否在数据库中存在
    if (result.length > 0 && currentTimer < (new Date(result[0].timeout)).getTime()) {
      await next(); // token匹配，继续执行下一个中间件或路由处理函数
    } else {
      ctx.status = 401; // token不匹配，返回未授权状态码
      ctx.body = 'Invalid token';
    }
};

module.exports = {
    checkTokenMiddleware
}